#include <vector>
#include "structures.h"
#include "io.h"
#include "unit_locator.h"
#include "game_bot.h"

using namespace std;

/**
 * Program entry point
 */
int main(int argc, char *argv[])
{
	if (argc == 2)
	{
		freopen(argv[1], "r", stdin);
	}

	game_settings settings;
	settings = read_settings();

	vector<unit_type> unit_types;
	game_field field;
	vector<state> states;

	unit_locator locator(settings);

	//
	// Locate units on the field
	//

	vector<zone> zones = read_zones(settings.field_radius);
	unit_types = read_unit_types();

	settings.max_health = unit_types[0].max_health;
	for (unsigned i = 1; i < unit_types.size(); i++)
	{
		if (unit_types[i].max_health > settings.max_health)
		{
			settings.max_health = unit_types[i].max_health;
		}
	}

	settings.max_health = 0;

	vector<available> available_units = read_available_units();
	write_locations(locator.locate_units(available_units, zones), settings.field_radius);

	game_bot bot(settings, unit_types, states, field);

	//
	// Play game
	//

	while (true)
	{
		move enemy_move = read_move(settings.field_radius);
		if (enemy_move.unit_count == -2)
		{
			break;
		}

		read_situation(states, field, settings.field_radius);
		
		for (unsigned i = 0; i < states.size(); i++)
		{
			field[states[i].x][states[i].y] = states[i];
		}

		write_move(bot.make_move(enemy_move), settings.field_radius);
		//cout << "0 0 0 0 0 0 0" << endl;
	}

	return 0;
}